package com.wushixin.springdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wushixin.springdemo.controller.utils.CustomException;
import com.wushixin.springdemo.dao.TypeMapper;
import com.wushixin.springdemo.pojo.Book;
import com.wushixin.springdemo.pojo.TypeInfo;
import com.wushixin.springdemo.service.BookService;
import com.wushixin.springdemo.service.TypeService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, TypeInfo> implements TypeService {

    @Autowired
    private BookService bookService;

    @Override
    public void removeWithBook(Integer id) {
        // 查图书列表有没有绑定的信息
        val bookLambdaQueryWrapper = new LambdaQueryWrapper<Book>();
        bookLambdaQueryWrapper.eq(id!=null,Book::getTypeId,id);

        val count = bookService.count(bookLambdaQueryWrapper);
        // 如果有，则删除失败
        if (count > 0) {
            throw new CustomException("关联了图书，不能删除");
        }
        // 没有，直接删除
        this.removeById(id);
    }
}
